package com.triangle.cashier;

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.media.MediaRouter;
import android.view.Display;
import android.view.WindowManager;

import com.triangle.cashier.bean.Order;

/**
 * 描述：客户屏幕显示内容工具类
 * 创建人：zxm
 * 创建时间： 2017/12/20 on 15:04.
 */

public class CustomerEngine {
    private static CustomerEngine instance;
    // 获取设备上的屏幕
    private DisplayManager mDisplayManager;// 屏幕管理器
    private Display[] displays;// 屏幕数组
    private CustomerDisplay mCustomerDisplay;   //（继承Presentation）

    private CustomerEngine(Context context) {
        MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
        mDisplayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO);
//        displays = mDisplayManager.getDisplays();
//        if (null == mCustomerDisplay && displays.length > 1) {
//            mCustomerDisplay =  new CustomerDisplay(context, displays[1]);// displays[1]是副屏
//            mCustomerDisplay.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//            mCustomerDisplay.show();
//        }

        if (route != null) {
            Display presentationDisplay = route.getPresentationDisplay();
            mCustomerDisplay = new CustomerDisplay(context, presentationDisplay);// displays[1]是副屏
//            mCustomerDisplay.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
            mCustomerDisplay.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
            mCustomerDisplay.show();

        }

    }

    /**
     * 单例模式，创建的时候把界面绑定到第二个屏幕中
     *
     * @param context 这里需要传入getApplicationContext(),就能实现全局双屏异显
     * @return
     */
    public static CustomerEngine getInstance(Context context) {
        if (instance == null) {
            instance = new CustomerEngine(context);
        }
        return instance;
    }

    public static void colose() {
        instance = null;
    }

    /**
     * 显示客户购买的商品集合
     */
    public void setProductList(Order order) {
        if (mCustomerDisplay != null) {
            mCustomerDisplay.setProductList(order);
        }
    }

    public void refresh(Order order) {
        if (mCustomerDisplay != null) {
            mCustomerDisplay.refresh(order);
        }
    }
}
